package com.stox.advice;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import com.stox.dto.exception.StoxException;
import com.stox.dto.web.Message;
import com.stox.dto.web.MessageType;
import com.stox.dto.web.Response;

@ControllerAdvice(annotations = RestController.class)
public class RestControllerExceptionHandlerAdvice extends ResponseEntityExceptionHandler {
    private static final Logger LOG = LoggerFactory.getLogger(RestControllerExceptionHandlerAdvice.class);

    @ExceptionHandler(StoxException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ResponseBody
    public Response<Void> handleException(StoxException exception) {
    	LOG.error("Exception : ", exception);
        Response<Void> response = new Response<>();
        response.getMessages().add(new Message(exception.getMessage(), MessageType.ERROR));
        return response;
    }

}